<template>
	<view class="content bg-content">
		<z-paging ref="paging" :loading-more-enabled="false" :auto-hide-loading-after-first-loaded="false" empty-view-img="/static/nodata1.png" :empty-view-img-style="{width:'410rpx',height:'278rpx',marginBottom:'50rpx'}"  v-model="dataList" @query="getList" @onRefresh="onRefresh">
			<view class="list">
				<view class="flex-bt wrap">
					<view class="flex">
						<view>总库存：</view>
						<view>{{goods_storage}}</view>
					</view>
					<view class="blue" @tap="show = true">修改</view>
				</view>
				<view class="wrap " v-for="(item,index) in dataList" :key="item.id" @tap="click(item.id)">
					<view class="flex" style="margin-bottom: 30rpx;">
						<view class="img-box">
							<image :src="item.logo" mode="aspectFill" class="img"></image>
						</view>
						<view class="font">
							<view class="h3 line1" style="margin-bottom: 20rpx;">
								{{item.name}}
							</view>
							<view class='p' v-if="item.seller_storage">
								当前库存：{{item.seller_storage.goods_storage}} <br>
								销量：{{item.seller_storage.goods_salenum}}
							</view>
						</view>
					</view>
					<u-input border type="number" class="inp" v-model="item.goods_storage" placeholder="请输入分配的库存数量"/>
					
				</view>
			</view>
			<view class="com-bot-box">
				<view class="container flex-ct">
					<view class="bg-red com-btn flex-ct" @tap="$u.throttle(submit,1000)" style="flex: 1;">
						分配库存
					</view>
				</view>
			</view>
			
			<view class="com-hei"></view>
		</z-paging>
		
		<!-- 修改昵称弹窗 -->
		<u-modal v-model="show" @confirm="submit" title="修改库存" show-cancel-button>
			<view class="slot-content">
				<!-- 昵称 -->
				<input class="changeInp" placeholder-style="color: #A9A9A9" placeholder="请输入新的库存" maxlength="16" v-model="goods_storage">
			</view>
		</u-modal>
	</view>
</template>

<script>
	import { mapState, mapMutations, mapGetters } from 'vuex';
	import config from '@/config/config.js';
	import {goods_list,goods_category,inventory,storage,seller_list,detail} from '@/config/user/api.js';
	export default {
		data() {
			return {
				show:false,
				dateShow:false,
				loading:true,
				dataList:[],
				list: [],
				listTip: null,
				goods_id:'',
				goods_storage:'',
				info:'',
			}
		},
		components: {
		},
		onLoad(o) {
			if (!o.goods_id) {
			    this.$common.showTip('当前参数错误！');
			    setTimeout(() => {
			        this.$common.goback();
			    }, 2000);
			    return false;
			};
			this.goods_id = o.goods_id;
			this.$list.init(this);
			// this.$user.init(this, userInfo => {
			// });
			
		},
		
		onShow() {
			this.$refs.paging.reload();
		},
		computed: {
			...mapState({
			    userInfo: 'userInfo',
			})
		},
		methods: {	
			async getList(pageNo,pageSize,onRefresh = false){
				const that = this;
				let paramData = {
					goods_id:that.goods_id,
				};
				const res = await detail(paramData);
				if (res.code != 1) {
					that.$common.showTip(res.msg, 'none')
					return
				};
				that.goods_storage  = res.data.goods_storage;
				let list = res.data.seller_goods_storage || [];
				if (list.length) {
					list.forEach((el) => {
						el.goods_storage = '';
					});
					let setdata = that.$list.normalData(list, that);
					that.list = setdata.list;
					that.$refs.paging.complete(setdata.list);
				} else {
					that.$refs.paging.complete([]);
				}
			},
			
			async onRefresh(){
				this.loading = true;
			},
			async submit(){
				const that = this;
				let seller_storage_data =[];
				let list = that.list;
				list.forEach(el=>{
					seller_storage_data.push({
						store_id:el.id,
						goods_storage: el.goods_storage || 0,
					})
				})
				
				console.log(seller_storage_data);
				let paramData = {
					goods_id:that.goods_id,
					goods_storage: that.goods_storage,
					seller_storage_data: JSON.stringify(seller_storage_data)
				};
				console.log(paramData);
				const res = await storage(paramData);
				if (res.code != 1) {
					that.$common.showTip(res.msg,'none');
					return
				};
				that.$common.showTip('库存分配成功','none');
				that.getList();
			},
			
			// 跳转页面方法
			toUrl(url) {
				uni.navigateTo({
					url
				})
			},
			
			...mapMutations(['setUserInfo'])
		}
	}
</script>


<style scoped lang="scss">
	.list{
		.wrap{
			background: #fff;
			border-radius: 10rpx;
			margin-bottom: 16rpx;
			padding: 32rpx;
			position: relative;
			.img{
				width: 120rpx;
				height: 120rpx;
				margin-right: 32rpx;
			}
			.font{
				width: 0;
				flex: 1;
				font-size: $font-b;
				.h3{
					color: $black;
				}
				.p{
					margin-top: 8rpx;
					color: rgba(0,0,0,.3);
				}
			}
			.em{
				font-size: $font-s;
				color: #888;
				margin-left: 10rpx;
			}
		}
	}
	.com-bot-box{
		bottom: 0;
	}
	
	.changeInp{
		width: 544rpx;
		height: 96rpx;
		border-radius: 16rpx;
		font-size: 32rpx;
		text-align: center;
		color: $black;
		margin: 35rpx auto 53rpx;
		background: #F8FAFD;
	}
</style>